/**
 * NextVisual Inc.
 */
package net.nextvisual.cms.common.web;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 执行时间过滤器
 * 
 * @author 黄奇
 * 
 */
public class ProcessTimeFilter implements Filter {
    /**
     * 日志记录器
     */
    private final Logger log = LoggerFactory.getLogger(ProcessTimeFilter.class);
    /**
     * 请求执行开始时间
     */
    public static final String START_TIME = "_start_time";

    public void doFilter(ServletRequest req, ServletResponse response, FilterChain chain) throws IOException,
            ServletException {
        HttpServletRequest request = (HttpServletRequest) req;

        long time = System.currentTimeMillis();
        request.setAttribute(START_TIME, time);
        chain.doFilter(request, response);
        time = System.currentTimeMillis() - time;

        log.debug("process in: {} ms, request:{} ", request.getRequestURI(), time);
    }

    @Override
    public void destroy() {
        log.debug("destroy process time log filter");
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.debug("int destory process time log filter");
    }
}
